package ar.com.fiuba.ElectronicCircuitAnalyst.domain.impl;

import java.util.List;

import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.Circuit;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.CircuitElement;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.Connection;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.Lead;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.common.Specification;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.exceptions.InvalidCircuitElementException;

public class CircuitImpl extends CircuitElementImpl implements Circuit {

	private List<CircuitElement> circuitElements;
	private List<Connection> connections;
	private Specification<Circuit> validCircuitSpec;

	public CircuitImpl(String name, List<Lead> inputs, List<Lead> outputs, List<CircuitElement> circuitElements,
			Specification<Circuit> validCircuitSpec) throws InvalidCircuitElementException {
		this.name = name;
		this.inputLeads = inputs;
		this.outputLeads = outputs;
		this.circuitElements = circuitElements;
		this.validCircuitSpec = validCircuitSpec;

		validateCircuit();
	}

	public void simulate() {
		for (CircuitElement circuitElement : this.circuitElements) {
			circuitElement.simulate();
		}
		for (Connection connection : this.connections) {
			connection.connect();
		}
	}

	private void validateCircuit() throws InvalidCircuitElementException {
		if (!this.validCircuitSpec.isSatisfiedBy(this))
			throw new InvalidCircuitElementException("Circuit doesn't satisfy specification.");
	}

	public void setConnections(List<Connection> connections) {
		this.connections = connections;
	}

}
